<script>
    /**
     * vuex管理登陆状态，具体可以参考官方登陆模板示例
     */
    import {
        mapMutations
    } from 'vuex';

    export default {
        methods: {
            ...mapMutations(['login']),
			checkVersion(){
					
					//获取版本号
					let version = plus.runtime.version;
					// plus.nativeUI.alert(version)
					let osname = plus.os.name;
					console.log("版本号如下",version);
					let userInfo = uni.getStorageSync("userInfo");
					let token = userInfo.token;
					 
					uni.request({
					    url: 'https://tea.jialianxin.cn/api/version/index/getVersion', 
					    method: 'GET',
					    data: {
					        token: token
					    },  
					    traditional: true, 
					    success: (res) => {
					        if (res.statusCode === 200) {
					            if (res.data.code === 1) {
									console.log(res.data.data);
									console.log(version);
					                if (res.data.data === null) {
					                    return;
					                }
					                if (version !== res.data.data.str_version) { 
					                    if (osname === 'Android') {
					                        uni.setStorage({
					                            key: 'download',
					                            data: res.data.data.downloadurl,
					                            success: () => {
													uni.navigateTo({
														url: '/pages/uploadApp/uploadApp'
													})
					                            }
					                        });
					                    } else {
					                        // uni.reLaunch({
					                        // 	url: '/pages/iOSupdateApp/iOSupdateApp'
					                        // });
					                    }
					                }
					            }
					        }
					
					    },
					    fail: (error) => {
					        // console.log(error);
					        if (error.errMsg == "request:fail timeout") { //网络请求超时
					            showWindow("网络不稳定，请检查网络设置");
					        } else if (error.errMsg == "request:fail 请求超时。") {
					            showWindow("网络不稳定，请检查网络设置");
					        } else if (error.errMsg == "request:fail ") {
					            showWindow("请求失败");
					        } else {
					            showWindow("加载失败");
					        }
					    },
					    complete: () => {}
					});
					
			},
			loopChecked(){
				//判断是否实名
				var userInfo = uni.getStorageSync('userInfo');
				
				//判断是否登录
				if (!userInfo) {
				  setTimeout(()=>{
					  this.loopChecked();
				  },10000)
				}else{
					this.checkVersion();
				}
			}
        },
        onLaunch: function() {
			
			let userInfo = uni.getStorageSync('userInfo') || '';
			if (userInfo.id) {
			    //更新登陆状态
			    uni.getStorage({
			        key: 'userInfo',
			        success: (res) => {
			            this.login(res.data);
			        }
			    });
			}
			
        },
        onShow: function() {
			//获取用户当前的经纬度即当前位置
			uni.getLocation({
			    type: 'wgs84',
				// geocode:true,
			    success: (res) => {
			        console.log('当前位置的经度：' + res);
					var address = {
						'longitude':res.longitude,
						'latitude':res.latitude,
					};
					//存缓存
					uni.setStorageSync('address',res);
					console.log(uni.getStorageSync('address'))
			    },
				fail: (error) => {
					console.log(error)
				}
			});
			
			setTimeout(()=>{
				  this.loopChecked();
			},10000)
        },
        onHide: function() {
            // console.log('App Hide')
        },
		onReady:function() {
			// console.log('fdasfsd')
			
		},
    }
</script>
<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
</style>
<style>
    /* 引入字体图标库 */
    @import './iconfont.css';
    @import '/wxcomponents/vant/common/index.wxss';
    /*每个页面公共css */
    page,
    view,
    text,
    swiper,
    image,
	
    navigator {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }

    page {
        height: 100%;
        /* 定义主题颜色 */
        --themeColor: #ceb685;
        /* 定义统一的字体大小 */
        font-size: 30rpx;
        width: 750rpx;
        /* background: #ededed; */
    }
	
</style>